package com.nimt.hqgl_idea.mapper;


import com.nimt.hqgl_idea.entity.Order;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

@Component(value = "orderMapper")
public interface OrderMapper {

    @Insert("insert into `order` values(null,#{dishName},#{userId},#{userName},#{price},#{quantity},#{cost},now(),0)")
    @Options(useGeneratedKeys = true,keyProperty = "id",keyColumn = "id")
    void insert(Order order);

    @Select("<script>" +
            "select * from `order`  " +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='userId!=null'> and userId = #{userId}</if>" +
            "<if test='keywords!=null'> and concat(dishName,userName,date) like concat('%',#{keywords},'%')</if>" +
            "</trim>" +
            " limit #{start},#{pageSize}" +
            "</script>")
    List<Order> selectAll(@Param("start") Integer start,
                          @Param("pageSize") Integer pageSize,
                          @Param("userId") Integer userId,
                          @Param("keywords") String keywords);

    @Select("<script>" +
            "select count(1) from `order` " +
            "<trim prefix = 'where' prefixOverrides = 'AND|OR'>" +
            "<if test='userId!=null'> and userId = #{userId}</if>" +
            "<if test='keywords!=null'> and dishName like concat('%',#{keywords},'%')</if>" +
            "</trim>" +
            "</script>")
    int count(Integer userId,
              @Param("keywords") String keywords);

    @Update("update `order` set state = 1 where id = #{id} ")
    int updateState(Integer id);

    @Delete("delete from `order` where id = #{id}")
    int delete(Integer id);
}
